package com.broadtext.common.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;

/**
 * <p>
 * Title: MD工具类
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2017
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author xubin
 * @version 1.0
 */

public class RmsUtil {
    /**
     * 
     * <p>
     * Description: 把jsonStr信息写入到response中
     * </p>
     * 
     * @param response 响应
     * @param jsonStr 需要写入的集合
     */
    public static void writeJsonStr(HttpServletResponse response, String jsonStr) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
    
    /**
     * 
     * <p>Description: 判断对象是否为空</p>
     * @param object 对象
     * @return true：空，false：非空
     */
    public static boolean isBlank(Object object){
        if(null == object){
            return true;
        }
        if(object.toString().equals("")){
            return true;
        }
        return false;
    }
    
    /**
     * 
     * <p>Description: null转字符串</p>
     * @param str 字符串
     * @return 字符串
     */
    public static String null2String(String str)
    {
        if(null==str)
            return "";
        return str;
    }
    
    /**
     * 
     * <p>
     * Description: 导出Excel
     * </p>
     * 
     * @param fileName 文件名
     * @param response 响应对象
     * @param ba 数据流
     */
    public static void exportExcel(String fileName, HttpServletResponse response, ByteArrayOutputStream ba) {
        ServletOutputStream os = null;
        try {
            response.setContentType("application/x-download");
            response.setContentLength(ba.size());
            response.setHeader("Content-disposition", "attachment; filename="
                    + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
            response.setCharacterEncoding("iso-8859-1");
            //设置header jquery 捕获成功标志
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            os = response.getOutputStream();
            ba.writeTo(os);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    /**
     * 
     * <p>
     * Description: 保存Excel
     * </p>
     * 
     * @param fileName 文件名
     * @param response 响应对象
     * @param ba 数据流
     */
    public static void saveExcel(String fileName, Workbook workbook) {
        FileOutputStream fout = null;
        try {
            String filePath = "d:" + File.separator + "excelTemp";
            File fileIn = null;
            fileIn = new File(filePath);
            //判断上传文件的保存目录是否存在
            if (!fileIn.exists() && !fileIn.isDirectory()) {
                //创建目录
                fileIn.mkdirs();
            }
            filePath = filePath + File.separator + fileName;
            fout = new FileOutputStream(filePath);
            workbook.write(fout);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            try {
                fout.flush();
                fout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 
     * <p>
     * Description: 下载文件
     * </p>
     * 
     * @param fileName 文件名
     * @param filePath 文件路径
     * @param response 响应
     */
    public static void download(String fileName, String filePath, HttpServletResponse response) {
        InputStream in = null;
        OutputStream out = null;
        try {
            //读取目标文件，通过response将目标文件写到客户端  
            //获取目标文件的绝对路径  
            String fullFileName;
            fullFileName = filePath.concat(File.separator).concat(fileName);
            in = new BufferedInputStream(new FileInputStream(fullFileName));
            byte[] buffer;
            buffer = new byte[in.available()];
            in.read(buffer);
            in.close();
            response.reset();
            //设置Content-Disposition 防止文件打开
            response.setContentType("application/octet-stream");
            //设置编码防止中文乱码
            response.setHeader("Content-Disposition", "attachment;filename="
                    + new String(fileName.getBytes("GB2312"), "ISO8859-1"));
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            response.setContentLength(buffer.length);
            out = response.getOutputStream();
            //写文件  
            out.write(buffer);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
